﻿
Imports System
Imports System.IO
Imports System.Data.SqlClient
Imports ProjectWorks.My.MySettings
Imports SldWorks

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'ShopPartsDataSet.Projects' table. You can move, or remove it, as needed.
        'Me.ProjectsTableAdapter.Fill(Me.ShopPartsDataSet.Projects)
        Try
            Me.ShopPartsDataSet.Clear()
            'TODO: This line of code loads data into the 'ShopPartsDataSet.Parts' table. You can move, or remove it, as needed.
            Me.ProjectsTableAdapter.Fill(Me.ShopPartsDataSet.Projects)
            'TODO: This line of code loads data into the 'ShopPartsDataSet.Parts' table. You can move, or remove it, as needed.
            Me.PartsTableAdapter.Fill(Me.ShopPartsDataSet.Parts)
        Catch ex As Exception
            MessageBox.Show("Something bad happened!  Unable to retrieve data." & vbNewLine & "Contact someone to fix it.", "Terror", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub
    Private Sub ProjectComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ProjectComboBox.SelectedIndexChanged
        Me.ProjectsBindingSource.IndexOf(ProjectComboBox.SelectedValue)
    End Sub
    Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click
        Me.Validate()
        Me.PartsBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.ShopPartsDataSet)
    End Sub
    Private Sub NewPartToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewPartToolStripButton.Click


        If (AssyCheckBox.CheckState = CheckState.Unchecked) Then
            If My.Settings.PartTemplateLocation = Nothing Then
                MessageBox.Show("A part template has not been selected. Please select a template in User Options.")
                Exit Sub
            End If

            Me.Validate()
            Me.PartsBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.ShopPartsDataSet)

            Dim swApp As Object
            Dim Part As ModelDoc2
            Dim templatePath As String = My.Settings.PartTemplateLocation

            swApp = GetObject("", "SldWorks.Application")



            Part = swApp.NewDocument(templatePath, 0, 0.0#, 0.0#)
            Part = swApp.ActiveDoc
            swApp.ActiveDoc.ActiveView.FrameLeft = 0
            swApp.ActiveDoc.ActiveView.FrameTop = 0
            swApp.ActiveDoc.ActiveView.FrameState = 1

            swApp.ActiveDoc.ActiveView.FrameState = 1
            Part.Extension.InsertScene("\scenes\02 studio scenes\66 light cards.p2s")

            Dim FilePath As String = LocationTextBox.Text
            Dim FileName As String
            If FilePath Is Nothing Then
                If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
                    FilePath = FolderBrowserDialog1.SelectedPath
                    FileName = (FilePath & "\" & PartNoTextBox.Text & ".SLDPRT")
                    Part.CustomInfo("Description") = DescriptionTextBox.Text
                    Part.CustomInfo("Project") = ProjectComboBox.SelectedValue.ToString
                    Part.CustomInfo("DrawnDate") = Date.Today
                    If File.Exists(FileName) Then
                        MessageBox.Show("Part already exists")
                    Else
                        Part.SaveAs2(FileName, 0, False, False)
                    End If


                End If
            Else
                FileName = (FilePath & "\" & PartNoTextBox.Text & ".SLDPRT")
                Part.CustomInfo("Description") = DescriptionTextBox.Text
                Part.CustomInfo("Project") = ProjectComboBox.SelectedValue.ToString
                Part.CustomInfo("DrawnDate") = Date.Today
                If File.Exists(FileName) Then
                    MessageBox.Show("Part already exists")
                Else
                    Part.SaveAs2(FileName, 0, False, False)
                End If
            End If




        End If

        If AssyCheckBox.CheckState = CheckState.Checked Then
            Dim retval As Integer = 0
            retval = MessageBox.Show("Assembly checkbox is checked.  Are you sure you want to create a new assembly?", "Create Assembly", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
            If retval = 1 Then
                If My.Settings.PartTemplateLocation = Nothing Then
                    MessageBox.Show("An assembly template has not been selected.  Please select a template in User Options.")
                    Exit Sub
                End If

                Me.Validate()
                Me.PartsBindingSource.EndEdit()
                Me.TableAdapterManager.UpdateAll(Me.ShopPartsDataSet)

                Dim swApp As Object
                Dim Part As Object
                Dim templatePath As String = My.Settings.AssemblyTemplateLocation

                swApp = GetObject("", "SldWorks.Application")

                '--------------------Block Recording--------------------

                Part = swApp.NewDocument(templatePath, 0, 0.0#, 0.0#)
                Part = swApp.ActiveDoc
                swApp.ActiveDoc.ActiveView.FrameLeft = 0
                swApp.ActiveDoc.ActiveView.FrameTop = 0
                swApp.ActiveDoc.ActiveView.FrameState = 1

                swApp.ActiveDoc.ActiveView.FrameState = 1
                Part.Extension.InsertScene("\scenes\02 studio scenes\66 light cards.p2s")

                Dim FilePath As String = LocationTextBox.Text
                Dim FileName As String
                If FilePath Is Nothing Then
                    If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
                        FilePath = FolderBrowserDialog1.SelectedPath
                        FileName = (FilePath & "\" & PartNoTextBox.Text & ".SLDASM")
                        Part.CustomInfo("Description") = DescriptionTextBox.Text
                        Part.CustomInfo("Project") = ProjectComboBox.SelectedValue.ToString

                        If File.Exists(FileName) Then
                            MessageBox.Show("Part already exists")
                        Else
                            Part.SaveAs2(FileName, 0, False, False)
                        End If

                    End If
                Else
                    FileName = (FilePath & "\" & PartNoTextBox.Text & ".SLDASM")
                    Part.CustomInfo("Description") = DescriptionTextBox.Text
                    Part.CustomInfo("Project") = ProjectComboBox.SelectedValue.ToString
                    If File.Exists(FileName) Then
                        MessageBox.Show("Part already exists")
                    Else
                        Part.SaveAs2(FileName, 0, False, False)
                    End If

                End If
            End If
        End If

    End Sub
    Private Sub btnOptions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOptions.Click
        OptionsForm.Show()
    End Sub
    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        'TODO: This line of code loads data into the 'ShopPartsDataSet.Parts' table. You can move, or remove it, as needed.
        Me.ProjectsTableAdapter.Fill(Me.ShopPartsDataSet.Projects)
    End Sub
    Private Sub DatagridViewToolStripBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DatagridViewToolStripBtn.Click
        DataGridViewForm.Show()
    End Sub
    Private Sub ProjectToolStripBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectToolStripBtn.Click
        ProjectForm.Show()
    End Sub
End Class





